<?php

namespace App\Http\Middleware\Auth;

use App\Http\Services\LoginService;
use Closure;

class MiniappNoLogin
{
    /**
     * 小程序登录验证
     */
    public function handle($request, Closure $next)
    {
        //防止非接口调用
        if (!$request->get("use", 0) && !preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"])) {
            return retFail("请求失败");
        }
        $token = $request->header('token');
        if (!empty($token)) {
            $login = new LoginService();
            $adminId = $login->authcode($token, 'DECODE');
            // $adminId = 717040 ;
            $request->offsetSet('wechat_admin_id', $adminId);
            define("UID", $adminId ?? "");
        } else {
            define("UID", "");
        }

        define("IS_VISUAL", 0);//模拟账号

        return $next($request);
    }
}
